package com.mmall.demo2.common.redis;

import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;

/**
 * @Author: jiaru
 * @Description: redis 操作封装类
 * @Date: Created on 2018/6/28
 * @Modify:
 */
@Slf4j
public class RedisPoolUtil {


    /**
     * 设置key的有效期
     * @param key 键
     * @param exTime 过期时间，单位是秒
     * @return
     */
    public static Long expire(String key, int exTime) {
        Jedis jedis = null;
        Long result = null;

        try {
            jedis = RedisPool.getJedis();
            result = jedis.expire(key, exTime);
        } catch (Exception e) {
            log.error("setEx key:{}，exTime:{}，error", key, exTime, e);
            RedisPool.returnBrokenResource(jedis);
            return result;
        }

        RedisPool.returnResource(jedis);
        return result;

    }

    /**
     * 设置键、值、过期时间
     * @param key       键       ：类型 -> String
     * @param value     值       ：类型 -> String
     * @param exTime    过期时间  ：单位 -> 秒
     * @return
     */
    public static String setEx(String key, String value, int exTime) {
        Jedis jedis = null;
        String result = null;

        try {
            jedis = RedisPool.getJedis();
            result = jedis.setex(key, exTime, value);
        } catch (Exception e) {
            log.error("setEx key:{}， value:{}，exTime:{}， error", key, value, exTime, e);
            RedisPool.returnBrokenResource(jedis);
            return result;
        }

        RedisPool.returnResource(jedis);
        return result;

    }


    /**
     * 设置键、值、过期时间
     * @param key       键       ：类型 -> byte[]
     * @param value     值       ：类型 -> byte[]
     * @param exTime    过期时间  ：单位 -> 秒
     * @return
     */
    public static String setEx(byte[] key, byte[] value, int exTime) {
        Jedis jedis = null;
        String result = null;

        try {
            jedis = RedisPool.getJedis();
            result = jedis.setex(key, exTime, value);
        } catch (Exception e) {
            log.error("setEx key:{}， bytes:{}，exTime:{}， error", key, value, exTime, e);
            RedisPool.returnBrokenResource(jedis);
            return result;
        }

        RedisPool.returnResource(jedis);
        return result;

    }
    /**
     * 向redis中放入值
     *
     * @param key   键
     * @param value 值
     * @return
     */
    public static String set(String key, String value) {
        Jedis jedis = null;
        String result = null;

        try {
            jedis = RedisPool.getJedis();
            result = jedis.set(key, value);
        } catch (Exception e) {
            log.error("set key:{} value:{} error", key, value, e);
            RedisPool.returnBrokenResource(jedis);
            return result;
        }

        RedisPool.returnResource(jedis);
        return result;
    }

    /**
     * 从redis中取值
     * @param key 键
     * @return
     */
    public static String get(String key) {
        Jedis jedis = null;
        String result = null;

        try {
            jedis = RedisPool.getJedis();
            result = jedis.get(key);
        } catch (Exception e) {
            log.error("set key:{} , error", key, e);
            RedisPool.returnBrokenResource(jedis);
            return result;
        }

        RedisPool.returnResource(jedis);
        return result;
    }

    /**
     * 从redis中取值
     * @param key 键
     * @return
     */
    public static byte[] get(byte[] key) {
        Jedis jedis = null;
        byte[] result = null;

        try {
            jedis = RedisPool.getJedis();
            result = jedis.get(key);
        } catch (Exception e) {
            RedisPool.returnBrokenResource(jedis);
            log.error("set key:{} , error", key, e);
            return result;
        }

        RedisPool.returnResource(jedis);
        return result;
    }


    /**
     * 从redis中删除数据
     * @param key 键
     * @return
     */
    public static Long del(String key) {
        Jedis jedis = null;
        Long result = null;

        try {
            jedis = RedisPool.getJedis();
            result = jedis.del(key);
        } catch (Exception e) {
            log.error("set key:{} , error", key, e);
            RedisPool.returnBrokenResource(jedis);
            return result;
        }

        RedisPool.returnResource(jedis);
        return result;
    }

    /**
     * 从redis中删除数据
     * @param key 键
     * @return
     */
    public static Long del(byte[] key) {
        Jedis jedis = null;
        Long result = null;

        try {
            jedis = RedisPool.getJedis();
            result = jedis.del(key);
        } catch (Exception e) {
            RedisPool.returnBrokenResource(jedis);
            log.error("set key:{} , error", key, e);
            return result;
        }

        RedisPool.returnResource(jedis);
        return result;
    }


    public static void main(String[] args) {
        Jedis jedis = RedisPool.getJedis();

        RedisPoolUtil.set("testKey", "1");

        String value = RedisPoolUtil.get("testKey");

        RedisPoolUtil.setEx("exKey", "ex", 60*10);

        RedisPoolUtil.expire("testKey", 60*10);

        RedisPoolUtil.del("testKey");

        System.out.println("ending");
    }
}
